package com.jike.noobmoney.mvp.view.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jike.noobmoney.R;
import com.jike.noobmoney.contants.ConstantValue;
import com.jike.noobmoney.entity.BannerEntity;
import com.jike.noobmoney.entity.BaseEntity;
import com.jike.noobmoney.entity.LoginUserEntity;
import com.jike.noobmoney.net.RetrofitHelper;
import com.jike.noobmoney.util.PerfectClickListener;
import com.jike.noobmoney.util.SPUtils;
import com.jike.noobmoney.util.ToastUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Create by xuke on 2017/10/28
 * 验证码登录
 */
public class ExchangeActivity extends BaseActivity {



    @BindView(R.id.et_code)
    EditText et_code;
    @BindView(R.id.tv_exchange)
    TextView tv_exchange;

    private Call<BaseEntity> loginCall;
    public String userid;

    @Override
    protected int layoutResID() {
        return R.layout.activity_exchange;
    }

    @Override
    protected void initData() {
        userid = SPUtils.getInstance().getString(ConstantValue.SpType.userid);
        tv_exchange.setOnClickListener(listener);
    }


    private PerfectClickListener listener = new PerfectClickListener() {
        @Override
        protected void onNoDoubleClick(View view) {
            int id = view.getId();
            if (id == R.id.tv_exchange) {
                String code = et_code.getText().toString();
                if (TextUtils.isEmpty(code)) {
                    et_code.requestFocus();
                    ToastUtils.showShortToastSafe("请输入兑换码");
                    return;
                }
                //兑换
                exchange();
            } else if (id == R.id.iv_back) {
                ExchangeActivity.this.finish();
            }

        }
    };


    /**
     * 兑换
     */
    private void exchange() {
        showProgress();
        String code = et_code.getText().toString().trim();
        loginCall = RetrofitHelper.getInstance().push(userid, code);
        loginCall.enqueue(new Callback<BaseEntity>() {
            @Override
            public void onResponse(Call<BaseEntity> call, Response<BaseEntity> response) {
                if (response != null) {
                    dismissProgress();
                    BaseEntity body = response.body();
                    if (body != null) {
                        String code = body.getCode();
                        if ("100".equals(code)) {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                            finish();
                        } else {
                            ToastUtils.showShortToastSafe(body.getRinfo());
                        }
                    }
                }
            }

            @Override
            public void onFailure(Call<BaseEntity> call, Throwable t) {
                Log.e("Login", t.getMessage());
                dismissProgress();
            }
        });


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (loginCall != null) {
            loginCall.cancel();
        }
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
